package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uzhie.jt808.JT808Constant;
import com.uzhie.jt808.jt1078.entity.JT_9101;
import com.uzhie.jt808.service.JT808Service;
import com.uzhie.media.MediaConstant;
import com.uzhie.media.service.MediaPostServiceImpl;
import com.uzhie.media.task.MediaChannelTypeOnLineNumTask;
import com.uzhie.media.task.MediaOnLineNumTask;
import com.uzhie.web.anno.Auth;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.service.DemoService;
import io.netty.buffer.ByteBufUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutionException;

/**
 * Created by TangFei on 2018/7/18.
 * controller层统一接收 <strong>requestBody</strong> <strong>jsonObject</strong> 统一返回 <strong>responseBody</strong>
 */
@RestController
@RequestMapping(value = "demoHttp")
public class DemoController extends CommController {

    @Autowired(required = false)
    private DemoService service;

    @Autowired(required = false)
    private JT808Service jt808Service;

    @Autowired(required = false)
    private MediaPostServiceImpl<Integer> mediaPostService;


    @Auth(tokenRequired = true)
    @RequestMapping(value = "test")
    public PayloadBean test(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) throws InstantiationException, IllegalAccessException {
        logger.info("controller 中的消息体：" + body);
        PayloadBean payloadBean = super.buildResponse(body);
        return payloadBean;
    }

    @Auth(tokenRequired = false)
    @RequestMapping(value = "test2")
    public PayloadBean test2(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) throws InstantiationException, IllegalAccessException {
        logger.info(body);
        throw new RuntimeException("test2 异常了");
    }

    @Auth(tokenRequired = false)
    @RequestMapping(value = "test3")
    public PayloadBean test3(@RequestBody(required = false) JSONObject body) throws InstantiationException, IllegalAccessException {
        logger.info(body);
        // 下发808指令
        byte[] bytes = new byte[1];
        bytes[0] = (byte) (body.getIntValue("obody") & 0XFF);
        JT_9101 order = new JT_9101(body.getIntValue("channel"), body.getIntValue("stream"), body.getIntValue("type"), Short.parseShort("1888"), Short.parseShort("8002"), "47.104.135.234");
        //请求执行下达
//        jt808Service.post(JT808Constant.MSG_ID.PLA_REQ_MONITOR, body.getString("vehicleNumber"), order.WriteToBytes());
        try {
            byte[] bytes1 = jt808Service.postWithResult(JT808Constant.MSG_ID.PLA_REQ_MONITOR, body.getString("vehicleNumber"), order.WriteToBytes(), byte[].class);
            logger.info("controller 中得到的结果是：" + ByteBufUtil.hexDump(bytes1));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("设备:" + body.getString("vehicleNumber") + "  通道 :" + body.getString("channel") + " 实时监控指令下发成功");
//        jt808Service.post(0x8801, body.getString("simNum"), bytes);
        return super.buildResponse(body);
    }

    @Auth(tokenRequired = false)
    @RequestMapping(value = "getOnLineMedia")
    public PayloadBean getOnLineMedia(@RequestBody(required = false) JSONObject requestBody) {
        JSONObject body = new JSONObject();
        body.put("channelNumber", requestBody.getInteger("channelNumber"));
        body.put("simNo", requestBody.getString("simNo"));
        body.put("playType", requestBody.getInteger("playType"));
        JSONObject object = new JSONObject();
        object.put("playType", requestBody.getInteger("playType"));
        JSONArray array = mediaPostService.postWithResult(MediaConstant.MSG_ID.PLA_REQ_CHANNEL_TYPE_NUMBER, object, MediaChannelTypeOnLineNumTask.class, JSONArray.class);
        logger.info("2.10 结果：" + array);
        Integer count = mediaPostService.postWithResult(MediaConstant.MSG_ID.PLA_REQ_ONLINE_CHANNEL, body, MediaOnLineNumTask.class, Integer.class);
        logger.info("2.5 结果：" + count);
        JSONObject resultObj = new JSONObject();
        resultObj.put(MediaConstant.MSG_ID.PLA_REQ_CHANNEL_TYPE_NUMBER + "", array);
        resultObj.put(MediaConstant.MSG_ID.PLA_REQ_ONLINE_CHANNEL + "", count);
        return super.buildResponse(resultObj);
    }

    @RequestMapping(value = "test4")
    public PayloadBean test4(@RequestBody(required = false) JSONObject requestBody) {
        System.out.println(requestBody);
        try {
            Thread.sleep(10 * 1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return buildResponse("test4");
    }

    @RequestMapping(value = "test5")
    public PayloadBean test5(@RequestBody(required = false) JSONObject requestBody) {
        System.out.println(requestBody);
        return buildResponse("test5");
    }

}
